/**
 * 
 */
package br.srv.full.faces.postalservices;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.srv.full.geoProcessing.entities.GPSPoint;
import br.srv.full.geoProcessing.entities.localidades.Locale;
import br.srv.full.geoProcessing.entities.localidades.LocaleType;
import br.srv.full.geoProcessing.entities.map.MapInformation;
import br.srv.full.geoProcessing.entities.map.MapType;
import br.srv.full.serviceLocator.ServiceLocator;
import br.srv.full.virtual.entities.faces.FacesAction;
import br.srv.full.virtual.postalServices.base.GeoInformationServices;

/**
 * @author Carlos Delfino
 * 
 */
public final class GeoManagerAddLocale extends Object {

	private final Log log = LogFactory.getLog(getClass());

	private Locale selectedLocale;

	private Float zoom;

	private MapType mapType = MapType.Hybrid;

	private LocaleType typeLocaleSelected;

	private Locale superLocaleSelectedLocale;

	private String nameSelectedLocale;

	private String acronymSelectedLocale;

	private List<Locale> superLocales;

	{
		ajustDefaultValues();
	}

	/**
	 * @return the selectedLocale
	 */
	public Locale getSelectedLocale() {
		return selectedLocale;
	}

	/**
	 * 
	 */
	private void ajustDefaultValues() {
		ajusteDefaulttypeLocaleSelected();

	}

	/**
	 * 
	 */
	private void ajusteDefaulttypeLocaleSelected() {
		List<LocaleType> l_localeTypes = getGeoInformationServices().getLocaleTypes();
		if (l_localeTypes != null && !l_localeTypes.isEmpty())
			typeLocaleSelected = l_localeTypes.get(0);
	}

	/**
	 * @param p_selected
	 *            the selectedLocale to set
	 */
	public void setSelectedLocale(Locale p_selected) {
		log.info(p_selected);
		selectedLocale = p_selected;
	}

	public LocaleType getTypeLocaleSelected() {
		Locale l_selected = getSelectedLocale();
		if (typeLocaleSelected == null && l_selected != null) {
			typeLocaleSelected = l_selected.getType();
			
		} 
		if(typeLocaleSelected == null )
			ajusteDefaulttypeLocaleSelected();
		
		return typeLocaleSelected;
	}

	public List<SelectItem> getTypesOfLocaleItens() {
		GeoInformationServices l_service = getGeoInformationServices();
		List<LocaleType> l_types = l_service.getLocaleTypes();

		List<SelectItem> l_itens;
		if (l_types != null && !l_types.isEmpty()) {
			l_itens = new ArrayList<SelectItem>(l_types.size());
			for (LocaleType l_localeType : l_types) {
				;
				l_itens.add(new SelectItem(l_localeType, l_localeType.getName(), l_localeType.getDescription()));
			}
		} else
			l_itens = new ArrayList<SelectItem>();

		return l_itens;
	}

	/**
	 * @return
	 */
	private GeoInformationServices getGeoInformationServices() {
		return ServiceLocator.getService(GeoInformationServices.class);
	}

	public String getAcronymSelectedLocale() {
		Locale l_selected = getSelectedLocale();
		if (acronymSelectedLocale == null && l_selected != null) {
			acronymSelectedLocale = l_selected.getAcronym();

		}
		return acronymSelectedLocale;
	}

	public String getNameSelectedLocale() {
		Locale l_selected = getSelectedLocale();
		if (nameSelectedLocale == null && l_selected != null) {
			nameSelectedLocale = l_selected.getName();

		}
		return nameSelectedLocale;
	}

	public Locale getSuperLocaleSelectedLocale() {
		Locale l_selected = getSelectedLocale();
		if (superLocaleSelectedLocale == null && l_selected != null) {
			superLocaleSelectedLocale = l_selected.getSuperLocale();

		}
		return superLocaleSelectedLocale;
	}

	public List<SelectItem> getSuperLocalesItens() {

		if (superLocales == null || superLocales.isEmpty()) {
			Locale l_selected = getSelectedLocale();

			superLocales = getGeoInformationServices().findSuperLocalesPossibles(l_selected);

			if (superLocales == null || superLocales.isEmpty()) {
				updateLocaleInformationsWithTypeSelected();
			}
		}

		List<SelectItem> l_itens;
		if (superLocales != null && !superLocales.isEmpty()) {
			l_itens = new ArrayList<SelectItem>();

			for (Locale l_locale : superLocales) {
				String l_description = l_locale.getAcronym() + " -  " + l_locale.getName() + " ( "
						+ l_locale.getDescription() + ")";
				l_itens.add(new SelectItem(l_locale, l_locale.getName(), l_description));
			}
		} else
			l_itens = new ArrayList<SelectItem>();
		return l_itens;
	}

	public String getMapKey() {
		return null;
	}

	public MapInformation getMapInformation() {
		return new MapInformation(new GPSPoint(-19.844439, -43.935324), getZoom(), getMapType());
	}

	public void updateLocaleInformationsWithTypeSelected() {
		log.info("updateLocaleInformationsWithTypeSelected");
		LocaleType l_selectedType = getTypeLocaleSelected();
		List<Locale> l_locales = getGeoInformationServices().findLocalesBelongsInLocaleType(l_selectedType);
		setSuperLocales(l_locales);
	}

	/**
	 * @param p_locales
	 */
	private void setSuperLocales(List<Locale> p_locales) {
		superLocales = p_locales;
		if (p_locales != null && !p_locales.isEmpty())
			superLocaleSelectedLocale = p_locales.get(0);
	}

	/**
	 * @return
	 */
	private MapType getMapType() {
		return mapType;
	}

	public void setMapType(MapType p_mapType) {
		mapType = p_mapType;
	}

	public Float getZoom() {
		return zoom;
	}

	public void setZoom(Float p_zoom) {
		zoom = p_zoom;
	}

	public GPSPoint point;
	private String lastMensageSaveSelectedLocale;

	/**
	 * @return the point
	 */
	public GPSPoint getPoint() {
		return point;
	}

	/**
	 * @param p_point
	 *            the point to set
	 */
	public void setPoint(GPSPoint p_point) {
		log.info(p_point);
		point = p_point;
	}

	public void setPoint(String p_point) {
		log.info(p_point);
	}

	public String getLastMensageSaveSelectedLocale() {
		return lastMensageSaveSelectedLocale;
	}

	public FacesAction saveSelectedLocale() {
		log.info("saveSelectedLocale");
		return null;
	}

	/**
	 * @param p_typeSelectedLocale
	 *            the typeSelectedLocale to set
	 */
	public void setTypeLocaleSelected(LocaleType p_typeSelectedLocale) {
		typeLocaleSelected = p_typeSelectedLocale;
	}

	/**
	 * @param p_superLocaleSelectedLocale
	 *            the superLocaleSelectedLocale to set
	 */
	public void setSuperLocaleSelectedLocale(Locale p_superLocaleSelectedLocale) {
		superLocaleSelectedLocale = p_superLocaleSelectedLocale;
	}

	/**
	 * @param p_nameSelectedLocale
	 *            the nameSelectedLocale to set
	 */
	public void setNameSelectedLocale(String p_nameSelectedLocale) {
		nameSelectedLocale = p_nameSelectedLocale;
	}

	/**
	 * @param p_acronymSelectedLocale
	 *            the acronymSelectedLocale to set
	 */
	public void setAcronymSelectedLocale(String p_acronymSelectedLocale) {
		acronymSelectedLocale = p_acronymSelectedLocale;
	}

	/**
	 * @param p_lastMensageSaveSelectedLocale
	 *            the lastMensageSaveSelectedLocale to set
	 */
	public void setLastMensageSaveSelectedLocale(String p_lastMensageSaveSelectedLocale) {
		lastMensageSaveSelectedLocale = p_lastMensageSaveSelectedLocale;
	}
}
